package org.kjk.mail;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class GMailWriteController extends AbstractController {
	private GMail gmail;

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// subject / receiver / content
		System.out.println("call mail write");
		String subject = request.getParameter("subject");
		String content = request.getParameter("content");
		String receiver = request.getParameter("receiver");
		String[] toAddr = null;
		if (receiver.contains(";")) {
			toAddr = receiver.split(";");
		} else {
			toAddr = new String[] { receiver };
		}
		System.out.println("subject :" + subject);
		System.out.println("content : " + content);
		System.out.println("addr : " + toAddr[0]);
		boolean res = gmail.sendGmail(toAddr, subject, content);
		if (res) {
			response.getWriter().println("success");
			System.out.println("send success");
		} else {
			response.getWriter().println("fail");
			System.out.println("send fail");
		}
		return null;
	}

	public void setGmail(GMail gmail) {
		this.gmail = gmail;
	}

}
